﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SDK10kWrapper
{
    /// <summary>
    /// ACTi SDK10000 Camera Class. Call Instance Method to Play Individual Camera. Call Static Class Method to Play All Camera.
    /// This class implement IDisposable, call "using" when you new it as local variable. Call DisposeAll() in Form.Closed or Control.Unload() to Dispose All Cameras & Reset Reference Counting to ZERO
    /// </summary>
    public partial class Camera
    {
        /// <summary>
        /// Change HERE to get Default Value When Init
        /// </summary>
        static readonly MEDIA_CONNECTION_CONFIG2 DEFAULT = new MEDIA_CONNECTION_CONFIG2
        {
            ContactType = (int)CONTACT_TYPE.CONTACT_TYPE_UNICAST_PREVIEW,

            UniCastIP = "194.36.79.201",
            MultiCastIP = "172.16.1.82",

            UserID = "Admin",
            Password = "123456",

            ControlPort = 6001,
            HTTPPort = 80,
            MultiCastPort = 5000,
            RegisterPort = 6000,
            StreamingPort = 6002,

            ChannelNumber = 0,
            PlayFileName = "",
            ConnectTimeOut = 3,
        };

        public string Name;
        public Panel Panel;
        public string UniCastIP;
        public string UserName;
        public string Password;
        public bool IsRecording;
        public bool IsPlaying;
        public string SavePath;
        IntPtr Handle;
        
        static List<Camera> cams = new List<Camera>();
        public static int Count
        {
            get { return count; }
        }
        static int count = 0;

        Camera(string name, Panel panel, string uniCastIP, string userName, string password, IntPtr handle, bool isRecording, bool isPlaying, string savePath)
        {
            count++;
            if (name == null) this.Name = "camera" + count.ToString(); else this.Name = name;
            if (savePath == null) this.SavePath = @"C:\Recorder\" + this.Name + ".avi"; else this.SavePath = savePath;
            this.Panel = panel;
            this.UniCastIP = uniCastIP;
            this.UserName = userName;
            this.Password = password;
            this.Handle = handle;
            this.IsRecording = isRecording;
            this.IsPlaying = isPlaying;
            cams.Add(this);
        }

        public Camera(Panel panel)
            : this(panel, DEFAULT.MultiCastIP)
        { }

        public Camera(Panel panel, string IP)
            : this(panel, IP, DEFAULT.UserID, DEFAULT.Password)
        { }

        public Camera(Panel panel, string IP, string saveFile)
            : this(panel, IP, DEFAULT.UserID, DEFAULT.Password, saveFile)
        { }

        public Camera(Panel panel, string IP, string userName, string passWord, string saveFile = null, string cameraName = null)
            : this(cameraName, panel, IP == null ? DEFAULT.UniCastIP : IP, userName == null ? DEFAULT.UserID : userName, passWord == null ? DEFAULT.Password : passWord, IntPtr.Zero, false, false, saveFile)
        { }

        internal MEDIA_CONNECTION_CONFIG2 MCC2
        {
            get
            {
                MEDIA_CONNECTION_CONFIG2 medcon = DEFAULT;
                medcon.UniCastIP = this.UniCastIP;
                medcon.UserID = this.UserName;
                medcon.Password = this.Password;
                return medcon;
            }
        }

        internal MEDIA_RENDER_INFO MRI
        {
            get
            {
                MEDIA_RENDER_INFO medren = new MEDIA_RENDER_INFO();
                medren.rect.top = 0;
                medren.rect.left = 0;
                medren.rect.right = this.Panel.Width;//DisplayRectangle.Right - this.PictureBox.DisplayRectangle.Left;
                medren.rect.bottom = this.Panel.Height;//DisplayRectangle.Bottom - this.PictureBox.DisplayRectangle.Top;
                medren.hWnd = this.Panel.Handle;
                return medren;
            }
        }

        /// <summary>
        /// For TESTING purpose only. Don't call this in RELEASE Version
        /// </summary>
        public void Test()
        {
            throw new NotImplementedException();
        }
    }
}
